#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2018-12-21 14:35:01

import threading
from threading import Lock


_base_lock = Lock()
_pos_lock  = Lock()
_base = 1


def _sum(x, y):
    print("strat _sum")
    # Time 1
    with _base_lock:
        # Time 3
        with _pos_lock:
            result = x + y
    return result


def _minus(x, y):
    print("start _minus")
    # Time 0
    with _pos_lock:
        # Time 2
        with _base_lock:
            result = x - y
    return result


t1 = threading.Thread(target=_sum, args=(1, 2))
t2 = threading.Thread(target=_minus, args=(1, 2))
t1.start()
t2.start()
t1.join()
t2.join()
